//
//  MyViewController.m
//  OurMenu
//
//  Created by 尹坤 on 15/10/10.
//  Copyright © 2015年 尹坤. All rights reserved.
//

#import "MyViewController.h"
#import "MyTableViewCell.h"
#import "DataBaseTool.h"
#import "DetailsViewController.h"
#import "SearchViewController.h"
@interface MyViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"我的收藏";
    
    tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 108) style:UITableViewStyleGrouped];
    tab.delegate = self;
    tab.dataSource = self;
    tab.rowHeight = 110;
    [self.view addSubview:tab];
    
    
}
- (void)viewWillAppear:(BOOL)animated
{
    //[super viewWillAppear:animated];
    [self.allDataArray removeAllObjects];
    
    DataBaseTool *tool = [[DataBaseTool alloc] init];
    self.dataArray = [tool selectDishes];
    
    DishesMadels *dish = [[DishesMadels alloc] init];
    for (int i = 0; i < self.dataArray.count; i++) {
        dish = self.dataArray[i];
        dish.albums = @[dish.Aalbums];
        dish.steps  = [tool selectSteps:dish.title];
        [self.allDataArray addObject:dish];
    }
    [tab reloadData];
    bottomView.alpha = 0;
    if (self.allDataArray.count == 0) {
        bottomView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        bottomView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:bottomView];
        
        imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0,150 , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 150-64 - 44)];
        imageview.image = [UIImage imageNamed:@"qqq"];
        UIImageView *addPic = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 40, 20, 80, 80)];
        // addPic.layer.cornerRadius = 10;
        // addPic.layer.masksToBounds = YES;
        addPic.image = [UIImage imageNamed:@"nocomment_loading_night"];
        addPic.backgroundColor = [UIColor whiteColor];
        
        UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [addButton setTitle:@"添加菜谱" forState:UIControlStateNormal];
        [addButton addTarget:self action:@selector(addMenu) forControlEvents:UIControlEventTouchUpInside];
        addButton.frame = CGRectMake(addPic.frame.origin.x, addPic.frame.origin.y+5+addPic.frame.size.height, 80, 40);
        addButton.backgroundColor = [UIColor orangeColor];
        addButton.layer.cornerRadius = 10;
        addButton.layer.masksToBounds = YES;
        
        [bottomView addSubview:addButton];
        [bottomView addSubview:addPic];
        [bottomView addSubview:imageview];

    }
}
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        self.dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)allDataArray
{
    if (!_allDataArray) {
        self.allDataArray = [NSMutableArray array];
    }
    return _allDataArray;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //NSLog(@"%ld",self.allDataArray.count);
    return self.allDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    DishesMadels *dish = self.allDataArray[indexPath.row];
    cell.dish = dish;
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsViewController *detail = [[DetailsViewController alloc] init];
    detail.dish = self.allDataArray[indexPath.row];
    [self.navigationController pushViewController:detail animated:NO];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        
        DishesMadels *dish = self.allDataArray[indexPath.row];
        DataBaseTool *tool = [[DataBaseTool alloc] init];
        [tool deleteDishes:dish.title];
        [tool deleteSteps:dish.title];
        [self.allDataArray removeObject:dish];
        [tab deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

        if (self.allDataArray.count == 0) {
            
            bottomView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
            bottomView.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:bottomView];
            
            imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0,150 , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 150-64 - 44)];
            imageview.image = [UIImage imageNamed:@"qqq"];
            UIImageView *addPic = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 40, 20, 80, 80)];
            // addPic.layer.cornerRadius = 10;
            // addPic.layer.masksToBounds = YES;
            addPic.image = [UIImage imageNamed:@"nocomment_loading_night"];
            addPic.backgroundColor = [UIColor whiteColor];
            
            UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [addButton setTitle:@"添加菜谱" forState:UIControlStateNormal];
            [addButton addTarget:self action:@selector(addMenu) forControlEvents:UIControlEventTouchUpInside];
            addButton.frame = CGRectMake(addPic.frame.origin.x, addPic.frame.origin.y+5+addPic.frame.size.height, 80, 40);
            addButton.backgroundColor = [UIColor orangeColor];
            addButton.layer.cornerRadius = 10;
            addButton.layer.masksToBounds = YES;
            
            [bottomView addSubview:addButton];
            [bottomView addSubview:addPic];
            [bottomView addSubview:imageview];

            
        }
    
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"取消收藏";
}
- (void)addMenu
{
    SearchViewController *search = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
